package com.sijibao.amap;

import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.google.gson.Gson;
import com.yicai.sijibao.utl.ClientInfo;

public class LocationService20 extends Service implements AMapLocationListener {
	
	private LocationManagerProxy mAMapLocManager = null;

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		startLocation();
		return START_REDELIVER_INTENT;
	}  
	
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		stopLocation();
		super.onDestroy();
	}
	
	
	
	private void startLocation(){
		mAMapLocManager = LocationManagerProxy.getInstance(this);
		mAMapLocManager.setGpsEnable(true);
		/*
		 * mAMapLocManager.setGpsEnable(false);//
		 * 1.0.2版本新增方法，设置true表示混合定位中包含gps定位，false表示纯网络定位，默认是true Location
		 * API定位采用GPS和网络混合定位方式
		 * ，第一个参数是定位provider，第二个参数时间最短是5000毫秒，第三个参数距离间隔单位是米，第四个参数是定位监听者
		 */
//		mAMapLocManager.requestLocationUpdates(
//				LocationProviderProxy.AMapNetwork, 7000, 100, this);
		mAMapLocManager.requestLocationData(LocationProviderProxy.AMapNetwork, -1, 100, this);
	
		
		
	}


	
	/**
	 * 销毁定位
	 */
	private void stopLocation() {
		if (mAMapLocManager != null) {
			mAMapLocManager.removeUpdates(this);
			mAMapLocManager.destory();
		}
		mAMapLocManager = null;
	}
	
	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onLocationChanged(AMapLocation location) {
		
		if(location!=null){
			Gson gson = new Gson();
			MyAmapLocation myLocation = MyAmapLocation.amapLocationToMy(location);
			LocationService.staticLocationJson = gson.toJson(myLocation);
			
			ClientInfo info=ClientInfo.build(getBaseContext());
			info.latitude=myLocation.latitude;
			info.longitude=myLocation.longitude;
			
			Intent intent = new Intent("com.sijibao.location.MyLocation.upload");
			intent.putExtra("locationType", "amap");
			intent.putExtra("location", LocationService.staticLocationJson);
			
			LocalBroadcastManager laBroadcastManager = LocalBroadcastManager.getInstance(getApplicationContext());
			laBroadcastManager.sendBroadcast(intent);
			
			stopLocation();
			stopSelf();
		}
//		Intent intent = new Intent("com.sijibao.AMapLocation");
////		intent.putExtra("latitude", location.getLatitude());
////		intent.putExtra("longitude", location.getLongitude());
////		intent.putExtra("province", location.getProvince());
//		intent.putExtra("location", location);
		
		
//		LocalBroadcastManager laBroadcastManager = LocalBroadcastManager.getInstance(getApplicationContext());
//		laBroadcastManager.sendBroadcast(intent);
		// TODO Auto-generated method stub
		
	}
	
	
//	private void upload(){
//		new MyThread(userID,getApplicationContext(),uploadUrl).start();
////		UploadData.upLoad(data, getApplicationContext(), uploadUrl);
//	}
	
	
	
//	public static MyLocation toMyLocation(AMapLocation location){
//		MyLocation mylocation = new MyLocation();
//		mylocation.locateTime = location.getTime()/1000;
//		mylocation.longitude = (long) (location.getLongitude()*1e6);
//		mylocation.latitude = (long) (location.getLatitude()*1e6);
//		mylocation.city = location.getCity();
//		mylocation.region =  location.getProvince();
//		mylocation.county =  location.getDistrict();
//		mylocation.street = "";
//		mylocation.streetNumber = "";
//		return mylocation;
//	}
//	
	

}
